package com.fd.wpx.sso.web.controllers;

import com.fd.wpx.authorizating.service.SysUserService;
import com.fd.wpx.blog.pojo.SysUser;
import com.fd.wpx.common.pojo.BlogResult;
import com.fd.wpx.sso.web.annotation.SameUrlData;
import org.apache.shiro.authc.credential.PasswordService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import java.util.Date;

@Controller
@RequestMapping("/user")
public class SysUserController {
    @Autowired
    private SysUserService sysUserService;
    @Autowired
    private PasswordService passwordService;
    @SameUrlData//这个方法要防止表单提交
    @RequestMapping("/register")
    public String register(SysUser user,Model model){
        user.setCtime(new Date());
        //设置用户是未锁定状态,0：锁定，1：未锁定
        user.setIslock("1");
        String password=passwordService.encryptPassword(user.getPassword());
        user.setPassword(password);
        BlogResult result=sysUserService.addUser(user);
        if(result.getStatus().equals(200))
        {
            model.addAttribute("info","用户注册成功");
        }
        return "login";
    }
    @RequestMapping("/exist")
    @ResponseBody
    public BlogResult exist(String name){
        BlogResult result=sysUserService.findUserByName(name);
        return result;
    }
}
